package com.ys.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ys.common.exception.BizCodeEnum;
import com.ys.gulimall.member.exception.PhoneExistException;
import com.ys.gulimall.member.exception.UserNameExistException;
import com.ys.gulimall.member.vo.MemberLoginVo;
import com.ys.gulimall.member.vo.MemberRegisterVo;
import com.ys.gulimall.member.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ys.gulimall.member.entity.MemberEntity;
import com.ys.gulimall.member.service.MemberService;
import com.ys.common.utils.PageUtils;
import com.ys.common.utils.R;

import javax.annotation.Resource;


/**
 * 会员
 *
 * @author yeswater
 * @email ruobairuo@gmail.com
 * @date 2021-07-17 15:19:59
 */

@Slf4j
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Resource
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
        public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
	
	@PostMapping("/register")
	public R register(@RequestBody MemberRegisterVo vo){
		try{
			memberService.register(vo);
			//异常机制：通过捕获对应的自定义异常判断出现何种错误并封装错误信息
		}catch (PhoneExistException e){
			return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
		}catch (UserNameExistException e){
			return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
		}
		return R.ok();
	}
	
	@PostMapping(value = "/oauth2/login")
	public R oauth2Login(@RequestBody SocialUser socialUser) throws Exception {
		
		MemberEntity memberEntity = memberService.login(socialUser);
		
		if (memberEntity != null) {
			log.info("登录成功="+ JSON.toJSONString(memberEntity));
			return R.ok().setData(memberEntity);
		} else {
			return R.error(BizCodeEnum.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMessage());
		}
	}
	
	@PostMapping("/login")
	public R login(@RequestBody MemberLoginVo vo){
		MemberEntity entity = memberService.login(vo);
		if (entity != null){
			return R.ok();
		}else {
			return R.error(BizCodeEnum.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMessage());
		}
		
	}

}
